package com.kexun.lxj;

public class Demo05 {
    public static void main(String[] args) {
        Thread t1=new Ticket7();
        Thread t2=new Ticket7();
        Thread t3=new Ticket7();
        Thread t4=new Ticket7();

        t1.start();
        t2.start();
        t3.start();
        t4.start();

    }
}

/*使用继承Thread的方法创建线程 锁对象*/
class Ticket7 extends Thread {

    private static int i = 20;

    Object obj= new Object();

    @Override
    public void run() {
        while (true) {

            /*解决不了线程安全问题
            * 因为odj不是共享对象 创建了四次*/
            synchronized (obj) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (i > 0) {
                    try {
                        System.out.println("这是" + Thread.currentThread().getName() + "卖出的第" + i-- + "张票");
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println(Thread.currentThread().getName()+"卖完了,关闭卖票窗口");
                    break;
                }
            }
        }
    }
}

